{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 2.用户按键实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：2.用户按键实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：用户按键实验\n",
    "我们可以通过用户按键选择对应的功能运行！<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "BtnPin = 19\n",
    "Gpin    = 5\n",
    "Rpin    = 6\n",
    "\n",
    "def setup():\n",
    "    GPIO.setwarnings(False)\n",
    "    GPIO.setmode(GPIO.BCM)       # Numbers GPIOs by physical location\n",
    "    GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output\n",
    "    GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output\n",
    "    GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Set BtnPin's mode is input, and pull up to high level(3.3V) \n",
    "\n",
    "    \n",
    "if __name__ == '__main__':     # Program start from here\n",
    "    setup()\n",
    "    try:\n",
    "        while True:\n",
    "            if GPIO.input(BtnPin) == True:\n",
    "                time.sleep(0.01)\n",
    "                if GPIO.input(BtnPin)==True:\n",
    "                    GPIO.output(Rpin,1)\n",
    "                    GPIO.output(Gpin,0)\n",
    "            elif GPIO.input(BtnPin) == False:\n",
    "                 time.sleep(0.01)\n",
    "                 if  GPIO.input(BtnPin) == False:\n",
    "                    while GPIO.input(BtnPin) ==True:\n",
    "                        pass\n",
    "                    GPIO.output(Rpin,0)\n",
    "                    GPIO.output(Gpin,1)                                              \n",
    "    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.\n",
    "        GPIO.cleanup()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
